<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { CustomMouseMenu } from '@/plugins/directive/contextmenu';
import { ElMessage, ElMessageBox } from 'element-plus';
import useStore from '@/store';
import { APPTYPE_TEXT_MAP } from '@/enums';
import { LicenseEnum } from '@/enums/licence';
import { WindowType } from '@/types/global/window';
import { props } from './app';
import Common from '@/utils';
defineOptions({ name: 'App' });
defineProps(props);
const treeRef = ref(null);
const tableRef = ref(null);
const loading = ref(true);
const treeLoading = ref(true);

const currentRow = ref(null);
const currentAppCatalogRow = ref(null);
const appCatlogs = ref(null);
const apps = ref(null);
const handleTableRowClick = row => {
  currentRow.value = row;
  nextTick(() => {
    tableRef.value.setCurrentRow(currentRow.value);
  });
};
const remove = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要删除的应用!');
    return;
  }
  let confirmMessage = '是否删除选中的应用?';
  // if (ids.constructor === Number) {
  //   confirmMessage = '是否删除应用（' + name + '）?';
  //   ids = [ids];
  // }

  ElMessageBox.confirm(confirmMessage, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // window.$api.module.remove(ids).then(res => {
      //   ElMessage.success('删除成功');
      //   query();
      // });
    })
    .catch(() => {});
};
const query = (): void => {
  window.$api.app.query().then(res => {
    apps.value = res.data;
    loading.value = false;
  });
};
const add = () => {
  const window: WindowType = { name: 'AppForm', component: 'AppForm', label: '应用添加', icon: 'edit', width: 600, height: 500, callBack: () => query() };
  useStore.window.windowAdd(window);
};
const edit = () => {
  if (!currentRow.value) {
    ElMessage.error('请选择要修改的应用!');
    return;
  }
  const window: WindowType = {
    name: 'AppForm' + currentRow.value.id,
    component: 'AppForm',
    label: '应用修改（' + currentRow.value.name + '）',
    icon: 'edit',
    width: 600,
    height: 500,
    params: { id: currentRow.value.id },
    callBack: () => query(),
  };
  useStore.window.windowAdd(window);
};

const menus = reactive([
  {
    label: '修改',
    icon: 'Edit',
    fn: () => {
      edit();
    },
    license: LicenseEnum.SYS_APP_MODIFY,
  },

  {
    label: '删除',
    icon: 'Delete',
    fn: () => {
      remove();
    },
    license: LicenseEnum.SYS_APP_REMOVE,
  },
]);
const showMouseMenu = (row, column, event) => {
  const ctx = CustomMouseMenu({
    el: event.currentTarget,
    mouse: event,
    ...{ menus },
  });
  ctx.$root.show();
  currentRow.value = row;
  tableRef.value.setCurrentRow(row);
};
onMounted(() => {
  query();
});
</script>

<template>
  <windows-window :window="window" class="app">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-apps-add-in-24-regular" :license="LicenseEnum.SYS_APP_ADD" @click="add">新建</windows-button>
        <windows-button icon="fluent-draw-shape-24-regular" :license="LicenseEnum.SYS_APP_MODIFY" split @click="edit">编辑</windows-button>
        <windows-button icon="fluent-delete-28-regular" :license="LicenseEnum.SYS_APP_REMOVE">删除</windows-button>
      </windows-button-group>
    </template>
    <el-table
      ref="tableRef"
      :data="apps"
      style="width: 100%"
      height="100%"
      :tree-props="{ hasChildren: 'hasChildren' }"
      row-key="id"
      highlight-current-row
      :default-expand-all="false"
      @row-contextmenu="showMouseMenu"
      @row-click="handleTableRowClick"
      v-loading="loading"
    >
      <el-table-column prop="name" label="名称" align="left" width="200">
        <template #default="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="100">
        <template #default="scope">
          <div style="display: flex; justify-content: center" v-if="scope.row.icon">
            <el-image style="width: 25px" :src="Common.getImage(scope.row.icon)" fit="contain" />
          </div>
          <span></span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="类型" align="center">
        <template #default="scope">
          <el-tag effect="dark">{{ APPTYPE_TEXT_MAP[scope.row.type] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="component" label="组件" align="center" minWidth="150"></el-table-column>
      <el-table-column label="许可证" prop="license" width="200" align="center">
        <template #default="scope">
          <el-tag type="danger" effect="dark" v-if="scope.row.license">{{ scope.row.license }}</el-tag>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column prop="showCorner" label="角落显示" align="center" width="100">
        <template #default="scope">
          <el-switch v-model="scope.row.showCorner" inline-prompt active-text="是" inactive-text="否" disabled></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="width" label="宽度" align="center" width="80"></el-table-column>
      <el-table-column prop="height" label="高度" align="center" width="80"></el-table-column>
      <el-table-column prop="sort" label="排序" align="center" width="80"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" minWidth="150"></el-table-column>
      <!-- <el-table-column prop="createdOn" label="创建日期" align="center" width="150"></el-table-column>
      <el-table-column prop="createdByName" label="创建人" align="center" width="100"></el-table-column>
      <el-table-column prop="updatedOn" label="更新日期" align="center" width="150"></el-table-column>
      <el-table-column prop="updatedByName" label="更新人" align="center" width="100"></el-table-column> -->
    </el-table>
  </windows-window>
</template>
<style lang="scss">
.app {
  .el-tree {
    .el-tree-node__content {
      height: 30px;
      margin: 3px 0px;
    }
  }
}
</style>
<style scoped lang="scss">
.app .el-table .cell {
  display: flex;
  align-items: center;
}
.cell_name {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.btngroup {
  height: 50px;
  display: flex;
  align-items: center;
  .el-icon {
    font-size: 18px;
    color: #909399;
    margin-right: 15px;
  }
}
.el-tree {
  width: 180px;
  .el-tree-node__content {
    height: 30px;
  }
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
</style>
